<?php

class Users extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function insertUser($user_id, $user_login, $user_email, $user_title, $requirements, $edit, $user_password = null) {
        $data['user_login'] = $user_login;
        $data['user_email'] = $user_email;
        $data['user_title'] = $user_title;

        $user_id = intval($user_id);

        if ($user_password != null) {
            $data['user_password'] = $user_password;
        }

        $this->db->trans_start();

        if ($user_id > 0) {
            $this->db->where('user_id', $user_id);
            $this->db->update('users', $data);
        } else {
            $query = $this->db->insert('users', $data);
            $user_id = $this->db->insert_id();
        }
        
        if (!$edit){
            $this->db->flush_cache();

            if (!is_array($requirements)) {
                $requirements = array();
            } else {
                $this->db->flush_cache();
                $this->db->where_not_in("basetype_id", $requirements);
                $this->db->where("user_id", $user_id);
                $this->db->delete("user_requirements");
            }

            foreach ($requirements as $basetype_id) {
                $this->db->where("basetype_id", intval($basetype_id));
                $this->db->where("user_id", intval($user_id));
                $query = $this->db->get('user_requirements');
                if ($query->num_rows() == 0) {
                    $this->db->flush_cache();

                    $req["user_id"] = intval($user_id);
                    $req["basetype_id"] = intval($basetype_id);
                    $this->db->insert('user_requirements', $req);
                }
            }
        }
        $this->db->trans_complete();

        return $this->db->trans_status();
        
    }

    function getByID($user_id) {
        $this->db->from('users AS U');
        $this->db->where('user_id', $user_id);
        $user = $this->db->get();
        if ($user->num_rows() > 0) {
            return $user->row();
        } else {
            return null;
        }
    }

    function getByLogin($user_login) {
        $this->db->from('users AS U');
        $this->db->where('user_login', $user_login);
        $user = $this->db->get();
        if ($user->num_rows() > 0) {
            return $user->row();
        } else {
            return null;
        }
    }

    function getByEmail($user_email) {
        $this->db->from('users AS U');
        $this->db->where('user_email', $user_email);
        $user = $this->db->get();
        if ($user->num_rows() > 0) {
            return $user->row();
        } else {
            return null;
        }
    }

    function getLikeName($term) {
        if (trim($term) != "") {
            $this->db->like("user_title", $term);
        }

        $this->db->order_by('user_title', "asc");
        return $this->db->get("users");
    }

    function getLikeEmail($term) {
        if (trim($term) != "") {
            $this->db->like("user_email", $term);
        }

        $this->db->order_by('user_email', "asc");
        return $this->db->get("users");
    }

    function getLikeUsername($term) {
        if (trim($term) != "") {
            $this->db->like("user_login", $term);
        }

        $this->db->order_by('user_login', "asc");
        return $this->db->get("users");
    }

    function getBasetypes($user_id) {
        $this->db->from("user_requirements AS UR");
        $this->db->where("UR.user_id", $user_id);

        $this->db->join("basetypes AS B", "B.basetype_id = UR.basetype_id");
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query;
        } else {
            return null;
        }
    }

    function deleteUser($user_id) {

        $this->db->trans_start();

        $this->db->where("user_id", $user_id);
        $this->db->delete("user_requirements");


        $this->db->where("user_id", $user_id);
        $this->db->delete("user_devices");


        $this->db->where("user_id", $user_id);
        $this->db->delete("user_orders");


        $this->db->where("user_id", $user_id);
        $this->db->delete("user_votes");


        $this->db->where("user_id", $user_id);
        $this->db->delete("feedbacks");


        $this->db->where("user_id", $user_id);
        $this->db->delete("users");

        if ($this->db->affected_rows() == 0){
            return false;
        }
        
        $this->db->trans_complete();

        
        return $this->db->trans_status();
    }

    function updateBasetypes($user_id, $basetypes) {
        if (!is_array($basetypes)) {
            return false;
        }

        $this->db->trans_start();

        //Remove basetypes no longer used
        $this->db->where_not_in('basetype_id', intval($basetypes));
        $this->db->where('user_id', intval($user_id));
        $this->db->delete('user_requirements');

        foreach ($basetypes as $basetype_id) {
            $this->db->where("basetype_id", intval($basetype_id));
            $this->db->where("user_id", intval($user_id));
            $query = $this->db->get('user_requirements');
            if ($query->num_rows() == 0) {
                $this->db->flush_cache();

                $req["user_id"] = intval($user_id);
                $req["basetype_id"] = intval($basetype_id);
                $this->db->insert('user_requirements', $req);
            }
        }

        $this->db->trans_complete();

        return $this->db->trans_status();
    }

    function deleteAllRequirements($user_id) {
        $this->db->trans_start();

        $this->db->where('user_id', intval($user_id));
        $this->db->delete('user_requirements');

        $this->db->trans_complete();
        return $this->db->trans_status();
    }
    
    function authenticate($username, $password){
        $this->db->where('user_login', $username);
        $this->db->where('user_password', $password);
        
        $user = $this->db->get('users');
        
        if ($user->num_rows() > 0){
            return $user->row();
        }
        else{
            return false;
        }
    }
    
    function updatePassword($user_id, $old_password, $new_password){
        $this->db->where('user_id', $user_id);
        $this->db->where('user_password', $old_password);
        $query = $this->db->get('users');
        
        if ($query->num_rows == 0){
            return false;
        }
        
        $this->db->trans_start();
        
        $this->db->where('user_id', $user_id);
        $this->db->set('user_password', $new_password);
        $this->db->update('users');
        
        $this->db->trans_complete();
        return $this->db->trans_status();
    }

}